package com.hiultra.archive.service.impl;

import java.util.List;

import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.hiultra.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hiultra.archive.domain.ArcCheckDetails;
import com.hiultra.archive.mapper.ArcCheckMapper;
import com.hiultra.archive.domain.ArcCheck;
import com.hiultra.archive.service.IArcCheckService;

/**
 * 盘点Service业务层处理
 *
 * @author ls
 * @date 2023-09-05
 */
@Service
public class ArcCheckServiceImpl implements IArcCheckService {
    @Autowired
    private ArcCheckMapper arcCheckMapper;

    /**
     * 查询盘点
     *
     * @param checkId 盘点主键
     * @return 盘点
     */
    @Override
    public ArcCheck selectArcCheckByCheckId(Long checkId) {
        ArcCheck arcCheck = arcCheckMapper.selectArcCheckByCheckId(checkId);
        List<ArcCheckDetails> arcBoxCheckDetailsList =new ArrayList<>();
        List<ArcCheckDetails> arcArcArchiveCheckDetailsList = new ArrayList<>();
        for (ArcCheckDetails arcCheckDetails: arcCheck.getArcCheckDetailsList()) {
            if ("01".equals(arcCheckDetails.getCheckType())){
                arcBoxCheckDetailsList.add(arcCheckDetails);
            }else if ("02".equals(arcCheckDetails.getCheckType())){
                arcArcArchiveCheckDetailsList.add(arcCheckDetails);
            }
        }
        arcCheck.setArcBoxCheckDetailsList(arcBoxCheckDetailsList);
        arcCheck.setArcArchiveCheckDetailsList(arcArcArchiveCheckDetailsList) ;
        return arcCheck;
    }

    /**
     * 查询盘点列表
     *
     * @param arcCheck 盘点
     * @return 盘点
     */
    @Override
    public List<ArcCheck> selectArcCheckList(ArcCheck arcCheck) {
        return arcCheckMapper.selectArcCheckList(arcCheck);
    }

    /**
     * 新增盘点
     *
     * @param arcCheck 盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertArcCheck(ArcCheck arcCheck) {
        if (arcCheck.getArcBoxCheckDetailsList().size() <= 0 && arcCheck.getArcArchiveCheckDetailsList().size() <= 0) {
            throw  new ServiceException("盘点数据为空，请检查重试！");
        }
        arcCheck.setCreateTime(DateUtils.getNowDate());
        int rows = arcCheckMapper.insertArcCheck(arcCheck);
        insertArcCheckDetails(arcCheck);
        return rows;
    }

    /**
     * 修改盘点
     *
     * @param arcCheck 盘点
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArcCheck(ArcCheck arcCheck) {
        arcCheck.setUpdateTime(DateUtils.getNowDate());
        arcCheckMapper.deleteArcCheckDetailsByCheckId(arcCheck.getCheckId());
        insertArcCheckDetails(arcCheck);
        return arcCheckMapper.updateArcCheck(arcCheck);
    }

    /**
     * 批量删除盘点
     *
     * @param checkIds 需要删除的盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcCheckByCheckIds(Long[] checkIds) {
        arcCheckMapper.deleteArcCheckDetailsByCheckIds(checkIds);
        return arcCheckMapper.deleteArcCheckByCheckIds(checkIds);
    }

    /**
     * 删除盘点信息
     *
     * @param checkId 盘点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcCheckByCheckId(Long checkId) {
        arcCheckMapper.deleteArcCheckDetailsByCheckId(checkId);
        return arcCheckMapper.deleteArcCheckByCheckId(checkId);
    }

    /**
     * 新增资产盘点子信息
     *
     * @param arcCheck 盘点对象
     */
    public void insertArcCheckDetails(ArcCheck arcCheck) {
//        List<ArcCheckDetails> arcCheckDetailsList = arcCheck.getArcCheckDetailsList();
//        Long checkId = arcCheck.getCheckId();
//        if (StringUtils.isNotNull(arcCheckDetailsList)) {
//            List<ArcCheckDetails> list = new ArrayList<ArcCheckDetails>();
//            for (ArcCheckDetails arcCheckDetails : arcCheckDetailsList) {
//                arcCheckDetails.setCheckId(checkId);
//                list.add(arcCheckDetails);
//            }
//            if (list.size() > 0) {
//                arcCheckMapper.batchArcCheckDetails(list);
//            }
//        }

        List<ArcCheckDetails> arcBoxCheckDetailsList = arcCheck.getArcBoxCheckDetailsList();
        List<ArcCheckDetails> arcArcArchiveCheckDetailsList = arcCheck.getArcArchiveCheckDetailsList();
        Long checkId = arcCheck.getCheckId();
        if (StringUtils.isNotNull(arcBoxCheckDetailsList)) {
            List<ArcCheckDetails> list = new ArrayList<ArcCheckDetails>();
            for (ArcCheckDetails arcCheckDetails : arcBoxCheckDetailsList) {
                arcCheckDetails.setCheckId(checkId);
                arcCheckDetails.setCheckType("01");
                list.add(arcCheckDetails);
            }
        }
        if (StringUtils.isNotNull(arcArcArchiveCheckDetailsList)) {
            List<ArcCheckDetails> list = new ArrayList<ArcCheckDetails>();
            for (ArcCheckDetails arcCheckDetails : arcArcArchiveCheckDetailsList) {
                arcCheckDetails.setCheckId(checkId);
                arcCheckDetails.setCheckType("02");
                list.add(arcCheckDetails);
            }
            if (list.size() > 0) {
                arcCheckMapper.batchArcCheckDetails(list);
            }
        }
    }
}
